import { Parameter } from "./Parameter";
import { must, any } from "../../core/logic";
import { Engine } from "../../Engine";
class Sampler extends Parameter{
    constructor(opts={}){
        super(opts);
        //运行参数
        this.gpu = {
            buffer: null,
            layout: null,
            entry: null
        }

        //布局部分
        this.visibility = must(opts.visibility);
        this.type = must(opts.type);
        this.updateLayout();
        //数据部分
        this.buffer(any(opts.data, {
            addressModeU: "repeat",
            addressModeV: "repeat",
            addressModeW: "repeat",
            magFilter: "linear",
            minFilter: "linear",
            mipmapFilter: "linear"
        }));
    }
    set visibility (v) {
        this._visibility = v;
        return this;
    }
    get visibility () {
        return this._visibility;
    }
    set type (v) {
        this._type = v;
        return this;
    }
    get type () {
        return this._type;
    }
    updateLayout () {
        this.gpu.layout = {
            binding: this.binding,
            visibility: this.visibility,
            sampler: {
                type: this.type
            },
        }
        if (this.gpu.buffer) {
            this.gpu.buffer.destroy();
            this.gpu.buffer = null;
        }
        return this;
    }
    buffer (data) {
        if (!data) {
            return;
        }
        this.update(data);
    }
    /**
     * @method
     */
    update (data) {
        if (this.gpu.buffer) {
            this.gpu.buffer.destroy();
        }
        this.gpu.buffer = Engine.instance.device.createSampler(data);
        this.gpu.entry = {
            binding: this.binding,
            resource: this.gpu.buffer
        }
    }
    destroy () {
        if (this.watchers.length<=0) {
            if(this.gpu && this.gpu.buffer){
                this.gpu.buffer=null;
            }
            return true;
        }
        return false;
    }
}
export {Sampler}